package com.justnow.offer;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * @author justnow
 * Created on 2020-09-18
 * Description
 */
public class Solution57 {

    public static void main(String[] args) {
        List<String> list = restoreIpAddress("25525511135");
        System.out.println(list);
    }
    public static List<String> restoreIpAddress(String s) {
        List<String> result = new ArrayList<>();
        StringBuilder ip = new StringBuilder();
        for (int a = 0; a < 4; a++) {
            for (int b = 0; b < 4; b++) {
                for (int c = 0; c < 4; c++) {
                    for (int d = 0; d < 4; d++) {
                        if(a + b + c + d == s.length() )
                        {
                            int n1 = Integer.parseInt(s.substring(0, a));
                            int n2 = Integer.parseInt(s.substring(a, a+b));
                            int n3 = Integer.parseInt(s.substring(a+b, a+b+c));
                            int n4 = Integer.parseInt(s.substring(a+b+c));
                            if(n1 <= 255 && n2 <= 255 && n3 <= 255 && n4 <= 255) {
                                ip.append(n1).append('.').append(n2)
                                        .append('.').append(n3).append('.').append(n4);
                                System.out.println(ip.toString());
                                if(ip.length() == s.length() + 3)
                                    result.add(ip.toString());
                                ip.delete(0, ip.length());
                            }
                        }

                    }
                }
            }
        }
        return result;
    }
}
